package com.example.daxiguaforharmony.game.core.renderers;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.RectFloat;

public class LabelAtlas extends Sprite {
    /** 数字之间X轴的间距 */
    public float spacingX = 0;
    public String text = "";
    public float fontSize = 60;

    private float _itemWidth = 60f;
    private float _itemHeight = 60f;
    private float _itemSize = 60f;
    private RectFloat _rectSrc = new RectFloat();
    private RectFloat _rectDst = new RectFloat();
    private float _itemScaleX = 1f;
    private float _itemScaleY = 1f;

    public LabelAtlas(PixelMapHolder pixelMapHolder, float itemWidth, float itemHeight, float itemSize) {
        super(pixelMapHolder);
        this._itemWidth = itemWidth;
        this._itemHeight = itemHeight;
        this._itemSize = itemSize;
        this._itemScaleX = itemWidth / itemSize;
        this._itemScaleY = itemHeight / itemSize;
    }

    @Override
    public void render(Canvas canvas, Paint paint) {
        int index = 0;
        for (int i = 0; i < this.text.length(); i++) {
            int n = Character.getNumericValue(this.text.charAt(i));
            if (n < 0 || n > 9) continue;
            this._rectSrc.left = n * this._itemWidth;
            this._rectSrc.top = 0f;
            this._rectSrc.right = this._rectSrc.left +  this._itemWidth;
            this._rectSrc.bottom = this._itemHeight;
            this._rectDst.left = this.x + index * (this.fontSize * this._itemScaleX) + index * this.spacingX;
            this._rectDst.top = this.y;
            this._rectDst.right = this._rectDst.left + (this.fontSize * this._itemScaleX);
            this._rectDst.bottom = this._rectDst.top + this.fontSize * this._itemScaleY;
            paint.setAlpha(this.alpha);
            canvas.drawPixelMapHolderRect(this._pixelMapHolder, this._rectSrc, this._rectDst, paint);
            index += 1;
        }
    }
}
